import {
	defineStore
} from 'pinia';
import {
	getCollectionApi,
	addorPutCollectionApi,
	delCollectionApi,
} from '@/api/account';

export const useAccountStore = defineStore('account', {
	state: () => {
		return {
			userId: '', // 用户Id
			collectionList: [],
		};
	},
	// 也可以这样定义
	// state: () => ({ })
	actions: {
		// 获取收藏列表
		async getCollection(params = {}) {
			let {
				data
			} = await getCollectionApi({
				user: this.userId,
				...params
			});

			if (data.data.code == 1) {
				this.collectionList = data.data.data.records;
			} else {
				throw Error(data.data.message);
			}
		},
		// 删除收藏
		async delCollection(id) {
			let {
				data
			} = await delCollectionApi({
				ids: JSON.stringify([id])
			});

			if (data.data.code == 1) {
				let index = this.collectionList.findIndex(item => item.id = id);
				this.collectionList.splice(index, 1);
				return data.data.code;
			} else {
				throw Error(data.data.message);
			}
		},
	},
	unistorage: true // 开启后对 state 的数据读写都将持久化
});
